/*
 * Copyright 2001-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package code.buildit.plugin.intention;

import code.buildit.plugin.BuildItActionHandler;
import code.buildit.plugin.BuildItResourceBundle;
import code.buildit.plugin.adapter.PsiAdapter;
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class BuildItIntention extends PsiElementBaseIntentionAction {
    public boolean isAvailable(@NotNull Project project, Editor editor, @Nullable PsiElement psiElement) {
        return isField(psiElement) || isClass(psiElement);
    }

    private boolean isClass(PsiElement psiElement) {
        return psiElement != null && psiElement instanceof PsiIdentifier && psiElement.getParent() instanceof PsiClass;
    }

    private boolean isField(PsiElement psiElement) {
        return psiElement != null && psiElement.getContext() instanceof PsiField;
    }

    public void invoke(@NotNull Project project, Editor editor, PsiFile psiFile) throws IncorrectOperationException {
        PsiElement psiElement = new PsiAdapter(editor.getProject()).findMatchingElement(psiFile, editor);
        processClassClick(editor, psiElement);
        processFieldClick(editor, psiElement);          

    }

    private void processClassClick(Editor editor, PsiElement psiElement) {
        if (psiElement != null && psiElement.getParent() instanceof PsiClass) {
            PsiClass psiClass = (PsiClass) psiElement.getParent();
            new BuildItActionHandler().doExecuteAction(new PsiAdapter(editor.getProject()).findClass(psiElement), psiClass.getFields(), false);
        }
    }

    private void processFieldClick(Editor editor, PsiElement psiElement) {
        PsiField psiField = new PsiAdapter(editor.getProject()).findField(psiElement);
        if (psiField != null)
            new BuildItActionHandler().doExecuteAction(new PsiAdapter(editor.getProject()).findClass(psiElement), new PsiField[]{psiField}, false);
    }

    @NotNull
    public String getText() {
        //noinspection UnresolvedPropertyKey
        return BuildItResourceBundle.message("buildit.name");
    }

    @NotNull
    public String getFamilyName() {
        //noinspection UnresolvedPropertyKey
        return BuildItResourceBundle.message("buildit.family.name");
    }

}
